package L628;

/**
 * @program: javase
 * @description:
 * @author: luolidong
 * @create: 2021-06-29 09:37
 * @version: 1.0
 */
public class FindNum53 {
    public int search(int[] nums, int target) {
        int l=0,r=nums.length-1;
        int n=-1;
        while(l<r){
            int mid=(l+r)/2;
            if(nums[mid]==target){
                n=mid;
                break;
            }else if(nums[mid]>target){
                r=mid-1;
            }else {
                l=mid+1;
            }
        }
        if(n==-1) return 0;
        int n1=n;
        int count=1;
        while (n-1>=0&&nums[n-1]==target){
            count++;
            n--;
        }

        while (n1+1<nums.length&&nums[n1+1]==target){
            count++;
            n1++;
        }
        return count;
    }

    public static void main(String[] args) {
        int search = new FindNum53().search(new int[]{5, 7, 7, 8, 8, 10}, 8);
        System.out.println("search = " + search);
    }
}
